﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using static System.Formats.Asn1.AsnWriter;

namespace OptinMonitorTest
{
    internal class Program
    {
        static void Main(string[] args)
        {

            var configBuilder = new ConfigurationBuilder();
            configBuilder.SetBasePath(Directory.GetCurrentDirectory());
            configBuilder.AddJsonFile("setting.json", false, true);
            var config = configBuilder.Build();

            var services = new ServiceCollection();
            services.Configure<MyOptions>(config);
            services.AddScoped<MonitorService>();
            services.AddScoped<SnapShortService>();
            services.AddScoped<OptionService>();

            using (var sp = services.BuildServiceProvider())
            {
                while(true)
                {
                    using(var scope = sp.CreateScope())
                    {
                        // 同一个Scope内配置文件发生改变，程序配置也会改变；
                        //var test = scope.ServiceProvider.GetService<MonitorService>(); 
                        
                        // 同一个Scope内配置文件发生改变，程序配置不会改变，等下一个Scop 开始后程序配置才会发生改变；
                        //var test = scope.ServiceProvider.GetService<SnapShortService>();
                        
                        // 配置在第一次加载后永远不会发生改变，那怕配置文件发生了变化；
                        var test = scope.ServiceProvider.GetService<OptionService>();
                        test.show();
                        Console.ReadKey();
                    }
                }

            }
        }
    }

    public class MyOptions
    {
        public string Option1 { get; set; }
        public int Option2 { get; set; }

        public override string ToString()
        {
            return string.Format("Option1：{0}，Option2：{1}", Option1, Option2);
        }
    }
}